

class DateUtils {

  constructor() {
    if (!DateUtils.instance) {
      DateUtils.instance = this;
    }
    return DateUtils.instance;
  }

  static createDate(dateStr) {
    return new Date(dateStr.replace(/\.|\-/g, '/'));
  }

  /**
   * 特殊格式时间转换成日期
   */
  dateStrToDate(dateStr) {
    console.log('转化前的时间： ' + dateStr);
    var bytes = dateStr.split('-');
    console.log(bytes);
    var date;
    if (bytes.length == 7) {
      date = new Date(100 * bytes[0] + parseInt(bytes[1], 10), bytes[2] - 1, bytes[3], bytes[4], bytes[5], bytes[6]);
    }
    console.log(this.getDateNumberString(date));
    return date;
  }

  /**
   * 特殊格式时间转换成7字节Byte Array
   */
  dateStr2bytes(dateStr) {
    console.log('转化后的时间： ' + dateStr);
    var bytes = new Array(dateStr.length / 2);
    for (var i = 0; i < bytes.length; i++) {
      bytes[i] = parseInt(dateStr.slice(2 * i, 2 * (i + 1)), 16);
    }
    // console.log(bytes);
    return bytes;
  }

  compareHourMinute(hourMinute1, hourMinute2) {
    let date = new Date();
    let date2 = new Date(date);
    let a = hourMinute1.split(":");
    let b = hourMinute2.split(":");
    console.log(date2.setHours(b[0], b[1]) - date.setHours(a[0], a[1]))
    return date.setHours(a[0], a[1]) >= date2.setHours(b[0], b[1]);
  }

  /**
   * 获取yyyyMMddHHmmss格式的时间字符串
   */
  getDateNumberString(date) {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()

    return [year, month, day, hour, minute, second].map(formatNumber).join('')
  }

  //创建临时电子钥匙时的时间专用
  getDateByDateAndTime(date, time) {
    var dateList = date.split('-');
    var year = parseInt(dateList[0]);
    var month = parseInt(dateList[1]) - 1;
    var day = parseInt(dateList[2]);
    var timeList = time.split(':');
    var hour = parseInt(timeList[0]);
    var minutes = parseInt(timeList[1]);
    var second = 0;
    var newDate = new Date(year, month, day, hour, minutes, second);
    console.log(this.formatAllTime(newDate));
    return newDate;
  }

  //获取date的日期部分字符串
  formatDate(date) {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()

    return [year, month, day].map(formatNumber).join('-')
  }

  //获取date的时间部分字符串
  formatTime(date) {
    const hour = date.getHours()
    const minute = date.getMinutes()

    return [hour, minute].map(formatNumber).join(':')
  }

  //获取date的中文格式字符串
  formatAllTime(date) {
    if (!date) {
      date = new Date();
      let da = new Date();
      da.getMilliseconds();
    }
    const year = date.getFullYear();
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()

    return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  }

  /**
   * 获取date时间的字符串，包括毫秒
   * @param {时间} date 
   */
  getTimeStrWithMill(date) {
    if (!date) {
      date = new Date();
    }
    const year = date.getFullYear();
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()
    const mill = date.getMilliseconds();

    return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':') + "." + mill
  }

}

function formatNumber(n) {
  n = n.toString();
  return n[1] ? n : '0' + n;
}

export {
  DateUtils
}
